package com.library.controller;

import com.library.entity.OaDepartment;
import com.library.mapper.OaDepartmentMapper;
import com.library.service.OaDepartmentServiceImpl;
import com.library.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ido
 * @since 2024-12-14
 */
@Controller
@RequestMapping("")
public class OaDepartmentController {

    public static final String prefix = "/api/oadepartment";

    //TODO 改成 子类也能 Autowired
    @Autowired
    private OaDepartmentServiceImpl oaDepartmentServiceImpl;

    @RequestMapping("/oadepartment.html")
    public ModelAndView adminBooks() {
        List<com.library.entity.OaDepartment> list = oaDepartmentServiceImpl.list(new com.library.entity.OaDepartment());
        ModelAndView modelAndView = new ModelAndView("oadepartment");
        return modelAndView;
    }

    @RequestMapping(path = prefix + "/list",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult list(@RequestBody OaDepartment oaDepartment) {
        List<OaDepartment> list = this.oaDepartmentServiceImpl.list(oaDepartment);
        return BaseResult.successWithData(list);
    }

    @RequestMapping(path = prefix + "/add",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult add(@RequestBody OaDepartment oaDepartment, HttpServletRequest request) {
        this.oaDepartmentServiceImpl.add(oaDepartment, request);
        return BaseResult.success();
    }

    @RequestMapping(path = prefix + "/delete",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult delete(@RequestBody OaDepartment oaDepartment) {
        this.oaDepartmentServiceImpl.delete(oaDepartment);
        return new BaseResult();
    }

    @RequestMapping(path = prefix + "/update",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult update(@RequestBody OaDepartment oaDepartment) {
        this.oaDepartmentServiceImpl.update(oaDepartment);
        return new BaseResult();
    }

    @RequestMapping(path = prefix + "/detail",
            method = RequestMethod.POST
    )
    @ResponseBody
    public BaseResult detail(@RequestBody OaDepartment oaDepartment) {
        OaDepartment oaDepartment1 = this.oaDepartmentServiceImpl.detail(oaDepartment);
        return BaseResult.successWithData(oaDepartment1);
    }

}
